package com.ash.select;

import com.ash.util.ConnectionUtil;

import java.sql.*;
import java.util.Objects;

/**
 * @encoding : UTF-8
 * @Time : 2020/12/3 16:40
 * @Author : Chen Chao
 * @File : connection.com.ash.select.Test01.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 查询记录数量
 */
public class Test01 {
    public static void main(String[] args) {
        Connection connection= ConnectionUtil.getConnection();
        Statement statement=null;
        ResultSet resultSet=null;
        String sql="Select COUNT(*) as total from student";
        /*4.创建sql语句执行对象*/

        try {
            statement = Objects.requireNonNull(connection).createStatement();
            /*5.执行sql语句，DML返回受影响行数*/
            resultSet = statement.executeQuery(sql);
            if(resultSet.next()){
                /*mysql作妖下从1开始*/
                int anInt = resultSet.getInt(1);
                /*也可以根据列名获取*/
                int total = resultSet.getInt("total");
                System.out.println("anInt="+anInt);
                System.out.println("total="+total);
            }
        } catch ( SQLException e) {
            e.printStackTrace();
        } finally {
           ConnectionUtil.close(connection,statement,resultSet);
        }
    }
}
